import React from "react";
import {Descriptions, Divider} from "antd";
import BaseComponent from "../../../../redare/components/BaseComponent";
import UserPresenter from "../../.gen/presenter/component/core/UserPresenter";
import {dateFormat} from "../../../../redare/utils/DateUtils";
import {getLoginUserInfo, isLoginUserInRootPlatform} from "../../utils/WorkbenchUtil";

export default class UserDetail extends BaseComponent {

    static defaultProps = {
        id: undefined,//用户ID
    };

    constructor(props) {
        super(props);
        this.presenter = new UserPresenter(this);
        this._isRootPlatform = isLoginUserInRootPlatform();
        this._user = getLoginUserInfo();
        this.state = {data: {}};
    }

    componentDidMount() {
        let {id} = this.props;
        this.showLoadingIndicator('加载中...');
        this.presenter.getUser({id, incMenu: true}, {}, (result) => {
            this.setState({data: result.data});
        });
    }

    showLoadingIndicator(text) {
        this.showSpin(text);
    }

    closeLoadingIndicator() {
        this.closeSpin();
    }

    render() {
        let {data} = this.state;
        return this.renderSpin(
            <>
                <Descriptions title="账号信息" bordered column={1} className="template-field-description">
                    <Descriptions.Item label="用户ID">{data.id}</Descriptions.Item>
                    <Descriptions.Item label="账号">{data.account}</Descriptions.Item>
                    <Descriptions.Item label="用户名">{data.name}</Descriptions.Item>
                    {
                        this._isRootPlatform && (
                            <Descriptions.Item label="系统平台">{data.platformName}（{data.platform}）</Descriptions.Item>
                        )
                    }
                    {
                        this._isRootPlatform && this._user.root && (
                            <Descriptions.Item label="用户平台">{data.userPlatform}</Descriptions.Item>
                        )
                    }
                    <Descriptions.Item label="菜单">{data.menuName}</Descriptions.Item>
                    <Descriptions.Item label="用户角色">{data.roleNames}</Descriptions.Item>
                    <Descriptions.Item label="管理员">{data.admin ? '是' : '否'}</Descriptions.Item>
                </Descriptions>
                <Divider/>
                <Descriptions title="账号状态" bordered column={1} className="template-field-description">
                    <Descriptions.Item label="状态">{data.status === 0 ? '正常' : '锁定'}</Descriptions.Item>
                    <Descriptions.Item label="描述">{data.statusMsg}</Descriptions.Item>
                    <Descriptions.Item label="需要重置密码">{data.needResetPwd ? '是' : '否'}</Descriptions.Item>
                    <Descriptions.Item label="最近登录时间">{dateFormat(data.lastLoginTime)}</Descriptions.Item>
                </Descriptions>
                <Divider/>
                <Descriptions title="用户信息" bordered column={1} className="template-field-description">
                    <Descriptions.Item label="电话">{data.mobile}</Descriptions.Item>
                    <Descriptions.Item label="email">{data.email}</Descriptions.Item>
                </Descriptions>
            </>
        );
    }
}
